package com.wolves.career.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wolves.career.entity.ProSchool;
import com.wolves.career.service.ProSchoolService;
import com.wolves.career.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 学校 前端控制器
 * </p>
 *
 * @author 麦翔盛
 * @since 2021-10-31
 */
@RestController
@CrossOrigin
@RequestMapping("/career/school")
public class ProSchoolController {


    @Autowired
    private ProSchoolService schoolService;

    // TODO: 2021/10/31 删除
    @DeleteMapping("/{id}")
    public R removeSchool(@PathVariable String id) {
        schoolService.removeById(id);
        return R.ok();
    }

    // TODO: 2021/10/31 添加
    @PutMapping("/school")
    public R addSchool(@RequestBody ProSchool school) {
        schoolService.save(school);
        return R.ok();
    }

    // TODO: 2021/10/31 修改
    @PostMapping("/school")
    public R updateSchool(@RequestBody ProSchool school) {
        schoolService.updateById(school);
        return R.ok();
    }

    // TODO: 2021/10/31 分页
    @PostMapping("/list/{pageNo}/{limit}")
    public R listSchool(@PathVariable long pageNo,
                        @PathVariable long limit,
                        @RequestBody(required = false) ProSchool school) {
        QueryWrapper<ProSchool> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(school.getSchoolName())) {
            queryWrapper.like("school_name", school.getSchoolName());
        }
        Page<ProSchool> page = new Page<>(pageNo, limit);
        IPage<ProSchool> schoolPage = schoolService.page(page, queryWrapper);
        return R.ok().data("records", schoolPage.getRecords()).data("total", schoolPage.getTotal());
    }

    @GetMapping("/{id}")
    public R getSchoolById(@PathVariable String id) {
        return R.ok().data("school", schoolService.getById(id));
    }

}

